<?php

namespace App\Mobile\Controllers;

include base_path() . '/vendor/autoload.php';
use EasyWeChat\Foundation\Application;
use DB;
use Auth;
use Validator;
use Session;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\User;
use App\WxPublicUser;
use App\Country;
use App\UserMatch;
use App\MatchLog;
use App\WorldChampion;

class WorldcupController extends Controller
{
    protected $config = [
        'debug' => true,
	    'app_id' => 'wx555be570d21859aa',
	    'secret' => 'f92678be7687d1e1b1471ca9c066f242',
        'token' => '',
    ];

	/**
	 * [championChoose 选择冠军]
	 * @return [type] [description]
	 */
	public function championChoose(User $user)
	{
        $app = new Application($this->config);
        $js = $app->js;
        $url = 'http://user.leyougame.cc/mobile/worldcup/champion';
		$exist = WorldChampion::where(['userid' => $user->userid])->first();
		if ($exist) {
			return view('mobile.worldcup.mychampion', compact('exist', 'js', 'url'));
		} else {
			$line1 = Country::whereIn('id', [1,2,3,4])->get();
			$line2 = Country::whereIn('id', [5,6,7,8])->get();
			$line3 = Country::whereIn('id', [9,10,11,12,13,14])->get();
			$line4 = Country::whereIn('id', [15,16,17,18,19,20])->get();
			$line5 = Country::whereIn('id', [21,22,23,24,25,26])->get();
			$line6 = Country::whereIn('id', [27,28,29,30,31,32])->get();
			return view('mobile.worldcup.index', compact('user', 'line1', 'line2', 'line3', 'line4', 'line5', 'line6', 'js', 'url'));
		}
	}

	/**
	 * [storeChoose 保存选择]
	 * @param  User   $user [description]
	 * @return [type]       [description]
	 */
	public function storeChoose(User $user, Request $request)
	{
		if (!$request->input('cid')) {
			return ['flag' => false, 'msg' => '请选择国家'];
		}
		if (time() >= strtotime('2018-06-19 00:00:00')) {
			return ['flag' => false, 'msg' => '竞猜已经结束'];
		}
		$exist = WorldChampion::where(['userid' => $user->userid])->first();
		if ($exist) {
			return ['flag' => false, 'msg' => '已经选择过了'];
		}
		$worldcup = new WorldChampion;
		$worldcup->userid = $user->userid;
		$worldcup->countryid = $request->input('cid');
		$worldcup->state = 0;
		$worldcup->save();
		return ['flag' => true];
	}

	/**
	 * [dayChoose 每日竞猜]
	 * @return [type] [description]
	 */
	public function dayChoose(User $user)
	{
    	$app = new Application($this->config);
        $js = $app->js;
        $url = 'http://user.leyougame.cc/mobile/worldcup/day_guess';
		$finish = MatchLog::where(['status' => 1])->get();//已开奖
		$games = MatchLog::whereIn('status', [0, 2])->get();//待开奖

		return view('mobile.worldcup.daychoose', compact('finish', 'games', 'user', 'js', 'url'));
	}

	/**
	 * [dayStore 每日竞猜]
	 * @return [type] [description]
	 */
	public function dayStore(User $user, Request $request)
	{
		if (!$request->input('match_id')) {
			return ['flag' => false, 'msg' => '请选择比赛'];
		}
		if (!$request->input('result')) {
			return ['flag' => false, 'msg' => '请选择结果'];
		}
		$match = MatchLog::where(['id' => $request->input('match_id')])->first();
		if ($match->match_time - time() <= 1800) {
			return ['flag' => false, 'msg' => '竞猜已经结束'];
		}
		$exist = UserMatch::where(['userid' => $user->userid, 'match_id' => $request->input('match_id')])->first();
		if ($exist) {
			return ['flag' => false, 'msg' => '您已经猜过了'];
		}
		$user_match = new UserMatch;
		$user_match->userid = $user->userid;
		$user_match->match_id = $request->input('match_id');
		$user_match->result = $request->input('result');
		$user_match->save();
		return ['flag' => true];
	}


}	